﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

namespace alexconsult.Modules.OfferManager.Controls
{
    public partial class ImageUploader : System.Web.UI.UserControl
    {
        bool first = true;
        bool createPreImg = false;
        public string[] files = new string[6];
        public string preImage = "";
        // --------------------------------------------------------------------------------------------------
        protected override void OnLoad(EventArgs e)
        {
            fu_1.Attributes.Add("onchange", "document.getElementById('"+fu_2.UniqueID.Replace("$","_")+"').style.display='';");
            fu_2.Attributes.Add("onchange", "document.getElementById('" + fu_3.UniqueID.Replace("$", "_") + "').style.display='';");
            fu_3.Attributes.Add("onchange", "document.getElementById('" + fu_4.UniqueID.Replace("$", "_") + "').style.display='';");
            fu_4.Attributes.Add("onchange", "document.getElementById('" + fu_5.UniqueID.Replace("$", "_") + "').style.display='';");
            fu_5.Attributes.Add("onchange", "document.getElementById('" + fu_6.UniqueID.Replace("$", "_") + "').style.display='';");
            base.OnInit(e);
        }
        // --------------------------------------------------------------------------------------------------
        public void Upload()
        {
            int pos = 0;
            if (fu_1.HasFile) { files[pos] = SaveImage(fu_1); pos++; }
            if (fu_2.HasFile) { files[pos] = SaveImage(fu_2); pos++; }
            if (fu_3.HasFile) { files[pos] = SaveImage(fu_3); pos++; }
            if (fu_4.HasFile) { files[pos] = SaveImage(fu_4); pos++; }
            if (fu_5.HasFile) { files[pos] = SaveImage(fu_5); pos++; }
            if (fu_6.HasFile) { files[pos] = SaveImage(fu_6); pos++; }
        }
        // --------------------------------------------------------------------------------------------------
        protected string SaveImage(FileUpload file)
        {
            string mask = "qwertyuiopasdfghjklzxcvbnm";
            Random rnd = new Random();
            string rndstr = "";
            for(int i=0; i<6; i++)
            {
                rndstr += mask[rnd.Next(0, 24)];
            }
            string fname = "";
            try
            {
                System.Drawing.Image loadedImg = System.Drawing.Image.FromStream(file.PostedFile.InputStream);

                fname = "/UserFiles/Images/Offers/Photos/" + Session.SessionID + "_" + rndstr + file.FileName;

                int h = 400;
                if (createPreImg) h = 110;

                int width = 0;
                if (loadedImg.Width < h)
                    width = loadedImg.Width;
                else
                    width = h;

                double d1 = Math.Round((double)loadedImg.Width / width, 4);
                double d2 = Math.Round((double)loadedImg.Height / d1, 4);
                int height = Convert.ToInt32(d2);

                Bitmap imgOutput = new Bitmap(width,height);
                imgOutput.MakeTransparent();
                imgOutput.MakeTransparent(Color.Black);

                Graphics newGraphics = Graphics.FromImage(imgOutput);
                newGraphics.Clear(Color.FromArgb(0, 255, 255, 255));

                newGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                newGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                newGraphics.DrawImage(loadedImg,0,0,width,height);
                newGraphics.Flush();

                if (file.FileName.ToLower().EndsWith(".jpg") || file.FileName.ToLower().EndsWith(".jpeg"))
                    imgOutput.Save(Server.MapPath(fname), System.Drawing.Imaging.ImageFormat.Jpeg);
                if (file.FileName.ToLower().EndsWith(".png"))
                    imgOutput.Save(Server.MapPath(fname), System.Drawing.Imaging.ImageFormat.Png);
                if (file.FileName.ToLower().EndsWith(".gif"))
                    imgOutput.Save(Server.MapPath(fname), System.Drawing.Imaging.ImageFormat.Gif);
                if (file.FileName.ToLower().EndsWith(".tiff"))
                    imgOutput.Save(Server.MapPath(fname), System.Drawing.Imaging.ImageFormat.Tiff);
            }
            catch { fname = "ERROR"; }

            if (first)
            {
                first = false;
                createPreImg = true;
                preImage = SaveImage(file);
                createPreImg = false;
            }

            return fname;
        }
        // --------------------------------------------------------------------------------------------------
    }
}